const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[],
    addressTotal:0,
  },

  gotoInput(){
    wx.navigateTo({
      url: '/pages/input/input',
    })
  },
  update(e){
    var name = e.currentTarget.dataset.item.name;
    var phone = e.currentTarget.dataset.item.phone;
    var address = e.currentTarget.dataset.item.address;
    var uid = e.currentTarget.dataset.item.uid;
    wx.navigateTo({
      url: '/pages/update/update?name=' + name + '&phone=' + phone + '&address=' + address + '&uid=' + uid,
    })
  },

  delete(e){
    var that = this;
    wx.request({
      url: httpUrl + '/address',
      method:'DELETE',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        uid: e.currentTarget.dataset.uid
      },
      success(res){
        if (res.data.code == 20021) {
          wx.showToast({
            title: '删除成功',
            icon:'none',
            duration:500
          })
          that.getAddressList();
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  getAddressList(){
    var that = this;
    wx.request({
      url: httpUrl + '/address',
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            addressList:res.data.data.rows,
            addressTotal:res.data.data.totalCount
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})